package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.AccountInfoService;
import com.qigao.mall.api.AccountLogService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.utils.excel.ExcelFormat;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.controller.AbstractExcelController;
import com.qigao.mall.web.vo.admin.merchant.AccountInfoQueryVo;
import com.qigao.mall.web.vo.admin.merchant.AccountLogCondVo;
import com.qigao.mall.web.vo.admin.merchant.SettleCondVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luiz
 * @Title: com.qigao.mall.web.controller.app.merchant
 * @ProjectName three-mall
 * @date 2020-04-2020/4/13 9:58
 * @Description:账户资金流水相关
 */
@RestController
@RequestMapping("/admin/accountLog/")
@Api(tags = "Admin账户资金流水相关")
public class AdminAccountLogController extends AbstractExcelController {
    private static Logger logger= LoggerFactory.getLogger(AdminAccountLogController.class);
    @Resource
    AccountLogService accountLogService;
    @PostMapping("/listPage")
    @ApiOperation(value = "账户资金流水分页查询列表", notes = "账户资金流水分页查询列表")
    public Result<PageInfo<AccountLogResultDto>> listPage(@RequestBody AccountLogCondVo accountLogCondVo, HttpServletRequest request){
        try{
            //参数校验
            AccountLogCondDto accountLogCondDto=new AccountLogCondDto();
            BeanUtils.copyProperties(accountLogCondVo,accountLogCondDto);
            if(StringUtils.isNotEmpty(accountLogCondVo.getStartDate())){
                accountLogCondDto.setStartDate(accountLogCondVo.getStartDate()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(accountLogCondVo.getEndDate())){
                accountLogCondDto.setEndDate(accountLogCondVo.getEndDate()+" 23:59:59");
            }
            return  accountLogService.listPage(accountLogCondDto);
        }catch (Exception e){
            logger.error("[账户资金流水分页查询列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    @GetMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void exportGet(AccountLogCondVo accountLogCondVo, HttpServletResponse response) {
        AccountLogCondDto accountLogCondDto=new AccountLogCondDto();
        BeanUtils.copyProperties(accountLogCondVo,accountLogCondDto);
        if(StringUtils.isNotEmpty(accountLogCondVo.getStartDate())){
            accountLogCondDto.setStartDate(accountLogCondVo.getStartDate()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(accountLogCondVo.getEndDate())){
            accountLogCondDto.setEndDate(accountLogCondVo.getEndDate()+" 23:59:59");
        }
        // 待导出数据
        List<AccountLogResultDto> orderResultDtos = accountLogService.findOrderResultByCond(accountLogCondDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "账户流水报表", excelUtils.getWorkbook());
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void export(@RequestBody AccountLogCondVo accountLogCondVo, HttpServletResponse response) {
        AccountLogCondDto accountLogCondDto=new AccountLogCondDto();
        BeanUtils.copyProperties(accountLogCondVo,accountLogCondDto);
        if(StringUtils.isNotEmpty(accountLogCondVo.getStartDate())){
            accountLogCondDto.setStartDate(accountLogCondVo.getStartDate()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(accountLogCondVo.getEndDate())){
            accountLogCondDto.setEndDate(accountLogCondVo.getEndDate()+" 23:59:59");
        }
        // 待导出数据
        List<AccountLogResultDto> orderResultDtos = accountLogService.findOrderResultByCond(accountLogCondDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "账户流水报表", excelUtils.getWorkbook());
    }

    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "日期"),
                new ExcelHeaderInfo(0, 0, 1, 1, "账户名称"),
                new ExcelHeaderInfo(0, 0, 2, 2, "资金动向"),
                new ExcelHeaderInfo(0, 0, 3, 3, "金额"),
                new ExcelHeaderInfo(0, 0, 4, 4, "所属公司")
        );
    }

    // 获取格式化信息
    private Map<String, ExcelFormat> getFormatInfo() {
        Map<String, ExcelFormat> format = new HashMap<>();
       /* format.put("id", ExcelFormat.FORMAT_INTEGER);
        format.put("categoryId", ExcelFormat.FORMAT_INTEGER);
    */
        return null;
    }
}
